"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 数据库启动过程中ctrl+c，再启动
Create At   : 2020/12/07
@zou_jialiang0505328126
Description :
    1、停止数据库gs_om -t stop
    2、启动数据库gs_om -t start
    3、启动过程中执行ctrl+c
    4、启动数据库gs_om -t start
Expect      :
    1、数据库停止成功
    2. 启动数据库成功
    3、数据库启动过程中，ctrl+c导致启动失败
    4、数据库启动成功
History     :
    Modified by @wan005 2021/8/16:修改查询进程与start执行顺序，规避start执行过快导致无法查询pid失败
    Modified by @wan005 2021/9/22:已线程的方式启动数据库，避免 查询pid与启动之间的时间差
"""

import unittest
import time
from yat.test import Node
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.ComThread import ComThread


class ModifyAudit(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Human_Case016.py 开始执行------------')
        self.dbPrimaryDbUser = Node(node='PrimaryDbUser')
        self.dbPrimaryRoot = Node(node='PrimaryRoot')
        self.Constant = Constant()
        self.common = Common('PrimaryRoot')
        self.dbname = 'postgres'

    def test_basebackup(self):
        self.log.info('--------------1、停止数据库gs_om -t stop-------------------------------')
        text = '----step1：停止数据库gs_om -t stop expect:成功----'
        result = self.commonshpri.stopDbCluster()
        self.assertTrue(result, '执行失败:' + text)

        self.log.info('--------------2、启动数据库gs_om -t start-------------------------------')
        text = '----step2：启动数据库gs_om -t start expect:失败----'
        start_thread = ComThread(self.commonshpri.startDbCluster, args=())
        start_thread.setDaemon(True)
        start_thread.start()

        self.log.info('----------------3、启动过程中执行ctrl+c--------------------')
        time.sleep(0.01)
        kill_thread = ComThread(self.common.kill_pid_keyword, args=(
            'start', '2', self.dbPrimaryDbUser,))
        kill_thread.setDaemon(True)
        kill_thread.start()

        kill_thread.join(60 * 60)
        result = kill_thread.get_result()
        self.log.info(result)

        start_thread.join(180)
        result = start_thread.get_result()
        self.assertFalse(result, '执行失败:' + text)

        self.log.info('---------------------4、启动数据库gs_om -t start---------------')
        text = '----step4：启动数据库gs_om -t start expect:成功----'
        result = self.commonshpri.startDbCluster()
        self.assertTrue(result)
        self.assertTrue(result, '执行失败:' + text)

    def tearDown(self):
        self.log.info('-------------------------环境清理------------------------')
        self.log.info('----------Opengauss_Reliability_Human_Case016.py 执行结束-----------------------')
